<?php
include('header.php');
$tw1 = mysql_query("SELECT * FROM `ytsubs` WHERE `user`='{$data['id']}' AND `active`='0'");
$tw = mysql_fetch_object($tw1);

if(isset($_POST['submit'])) {
$ext1 = mysql_query("SELECT * FROM `ytsubs` WHERE `y_name`='{$_POST['name']}' AND `user`='{$data['id']}'");
$ext = mysql_fetch_object($ext1);
if($ext->id != ""){
mysql_query("UPDATE `ytsubs` SET `active`='0' WHERE `y_name`='{$_POST['name']}' AND `user`='{$data['id']}'");
$mesaj = "<div class=\"success\">Youtube added successfully!</div>";
$h = 1;
}else{
$num1 = mysql_query("SELECT * FROM `ytsubs` WHERE `y_name`='{$_POST['name']}'");
$num = mysql_num_rows($num1);
if($num > 0){
$mesaj = "<div class=\"msg\"><div class=\"error\">Youtube Account Already Exists!</div></div>";
}else{ 
 $url = get_data("http://gdata.youtube.com/feeds/api/users/{$_POST['name']}?v=2");
 $xml = new SimpleXMLElement($url);
 $media = $xml->children( 'http://search.yahoo.com/mrss/' );
 $link = $xml->link[href];
 $yt = $xml->children( 'http://gdata.youtube.com/schemas/2007' );
 $name = $yt->username;
 $av = $media->thumbnail->attributes()->url;
 if ($name != "")
{
mysql_query("INSERT INTO `ytsubs` (user, y_name, y_link, y_av) VALUES('{$data['id']}', '{$name}', '{$link}', '{$av}')");
$mesaj = "<div class=\"msg\"><div class=\"success\">Youtube Account added successfully! </div></div>";
$h = 1;
}else{
$mesaj = "<div class=\"msg\"><div class=\"error\">Youtube Account dosen't exist! </div></div>";
}}}}?>
<div class="block medium right">
			<div class="top"><?if(isset($is_online)) {?>			
					<h1>Youtube Subscriber Account</h1>
			</div>
		<div class="content"><? echo $mesaj; ?> 
<? if($_GET['a'] == "cpc"){
if(isset($_POST['editcpc'])){
mysql_query("UPDATE `ytsubs` SET `cpc`='{$_POST['cpc']}' WHERE `y_name`='{$tw->y_name}' AND `user`='{$data['id']}'");
echo "<div class=\"msg\"><div class=\"success\">CPC successfully changed!</div></div>";
}
?>
<form method="post">
<fieldset>
	<p>
		<label>CPC*</label> <br/>
        <select class="styled" name="cpc">
		    <option value="1">1 Coin</option>
			<option value="2" <?if($tw->cpc == 2){?>selected<?}?>>2 Coins</option>
			<option value="3" <?if($tw->cpc == 3){?>selected<?}?>>3 Coins</option>
			<option value="4" <?if($tw->cpc == 4){?>selected<?}?>>4 Coins</option>
			<option value="5" <?if($tw->cpc == 5){?>selected<?}?>>5 Coins</option>
			<option value="6" <?if($tw->cpc == 6){?>selected<?}?>>6 Coins</option>
			<option value="7" <?if($tw->cpc == 7){?>selected<?}?>>7 Coins</option>
			<option value="8" <?if($tw->cpc == 8){?>selected<?}?>>8 Coins</option>
			<option value="9" <?if($tw->cpc == 9){?>selected<?}?>>9 Coins</option>
			<option value="10" <?if($tw->cpc == 10){?>selected<?}?>>10 Coins</option>
		</select>
	</p>
	<p><small><b>* Coins per Click.</b></small></p>	
    <p>
		<input type="submit" class="button gray small" value="Change CPC" name="editcpc" />
	</p>
</fieldset>
</form>
<? }else if($tw->y_name != ""){?>
<div style="float:left; margin-left: 5px;"><img src="<? echo $tw->y_av;?>" border="0" /></div>
<div style="float:left; margin-left: 5px;"><b><a href="http://youtube.com/<? echo $tw->y_name;?>" style="text-decoration: underline; color: #000;" target="_blank"><? echo $tw->y_name;?></a></b> (<a href="delete_yts.php?id=<? echo $tw->id;?>" style="text-decoration: none; color: red;">Delete</a>)<br><b>Coins:</b> <? echo $tw->points;?> (<a href="addcoins.php?x=ytsubs" style="text-decoration: none; color: blue;">Add</a>)<br><b>CPC:</b> <? echo $tw->cpc;?> (<a href="ysubconfig.php?a=cpc" style="text-decoration: none; color: blue;">Edit</a>)</div>
<?}else if(isset($_POST['submit']) && $h == 1){?>
<center><br><img src="<? echo $av;?>" border="0" /><br><b><? echo $_POST['name'];?></b></center><?}else{?>
<form method="post">
<table class="form">
	<tr>
		<td width="150px"><b>Youtube Username</b></td>
		<td>
			<input type="text" name="name" /><br />			
		</td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="submit" value="Submit" /></td>
	</tr>
</table>
<div class="msg"><div class="info">If you don't add your real Youtube username, you can't earn coins.</div></div>
</form>
<?}}else{?><script>document.location.href='index.php'</script><?}?>
				</div>
			</div>		
<?include('footer.php');?>